package com.ckzp.core.lc;

import java.security.MessageDigest;

/**
 * Created by 51594 on 2022/9/22.
 */
public class EncoderHandler {
    private static final String ALGORITHM = "MD5";
    private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public EncoderHandler() {
    }

    public static String encode(String algorithm, String str) {
        if(str == null) {
            return null;
        } else {
            try {
                MessageDigest e = MessageDigest.getInstance(algorithm);
                e.update(str.getBytes());
                return getFormattedText(e.digest());
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static String encodeByMD5(String str) {
        if(str == null) {
            return null;
        } else {
            try {
                MessageDigest e = MessageDigest.getInstance("MD5");
                e.update(str.getBytes());
                return getFormattedText(e.digest());
            } catch (Exception var2) {
                throw new RuntimeException(var2);
            }
        }
    }

    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);

        for(int j = 0; j < len; ++j) {
            buf.append(HEX_DIGITS[bytes[j] >> 4 & 15]);
            buf.append(HEX_DIGITS[bytes[j] & 15]);
        }

        return buf.toString();
    }

    public static String getSHA1(String str) {
        return encode("SHA1", str);
    }

    public static void main(String[] args) {
        System.out.println("111111 MD5  :" + encodeByMD5("111111"));
        System.out.println("111111 MD5  :" + encode("MD5", "111111"));
        System.out.println("111111 SHA1 :" + encode("SHA1", "111111"));
    }
}
